package com.ruoyi.grade.service.impl;

import java.math.BigDecimal;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.grade.mapper.GradeMapper;
import com.ruoyi.grade.domain.Grade;
import com.ruoyi.grade.service.IGradeService;

/**
 * 成绩详情Service业务层处理
 *
 * @author LinBei
 * @date 2025-05-15
 */
@Service
public class GradeServiceImpl implements IGradeService
{
    @Autowired
    private GradeMapper gradeMapper;

    /**
     * 查询成绩详情
     *
     * @param gradeId 成绩详情主键
     * @return 成绩详情
     */
    @Override
    public Grade selectGradeByGradeId(Long gradeId)
    {
        return gradeMapper.selectGradeByGradeId(gradeId);
    }

    /**
     * 查询成绩详情列表
     *
     * @param grade 成绩详情
     * @return 成绩详情
     */
    @Override
    public List<Grade> selectGradeList(Grade grade)
    {
        return gradeMapper.selectGradeList(grade);
    }

    /**
     * 新增成绩详情
     *
     * @param grade 成绩详情
     * @return 结果
     */
    @Override
    public int insertGrade(Grade grade)
    {
        return gradeMapper.insertGrade(grade);
    }

    /**
     * 修改成绩详情
     *
     * @param grade 成绩详情
     * @return 结果
     */
    @Override
    public int updateGrade(Grade grade)
    {
        return gradeMapper.updateGrade(grade);
    }

    /**
     * 批量删除成绩详情
     *
     * @param gradeIds 需要删除的成绩详情主键
     * @return 结果
     */
    @Override
    public int deleteGradeByGradeIds(Long[] gradeIds)
    {
        return gradeMapper.deleteGradeByGradeIds(gradeIds);
    }

    /**
     * 删除成绩详情信息
     *
     * @param gradeId 成绩详情主键
     * @return 结果
     */
    @Override
    public int deleteGradeByGradeId(Long gradeId)
    {
        return gradeMapper.deleteGradeByGradeId(gradeId);
    }

    @Override
    public void calculateClassRanking() {
        // 调用 MySQL 存储过程
        gradeMapper.calculateClassRanking();
    }

    @Override
    public List<Grade> selectGradeList_handteacher(String teacherId) {
        return gradeMapper.selectGradeList_handteacher(teacherId);
    }

    @Override
    public List<Grade> selectGradeList_Xiteacher(String teacherId) {
        return gradeMapper.selectGradeList_Xiteacher(teacherId);
    }

    @Override
    public List<Grade> selectGradeList_teacher(String teacherId) {
        return gradeMapper.selectGradeList_teacher(teacherId);
    }

    @Override
    public List<Grade> selectGradeList_student(String teacherId) {
        return gradeMapper.selectGradeList_student(teacherId);
    }

    @Override
    public Map<String, Integer> getGradeDistribution() {
        List<Map<String, Object>> result = gradeMapper.countGradesByRanges();
        if (result == null || result.isEmpty()) {
            return Collections.emptyMap();
        }

        Map<String, Integer> distribution = new HashMap<>();
        Map<String, Object> data = result.get(0);

        distribution.put("小于60", data.get("lessThan60") != null ? ((BigDecimal) data.get("lessThan60")).intValue() : 0);
        distribution.put("60-69", data.get("sixtyToSixtyNine") != null ? ((BigDecimal) data.get("sixtyToSixtyNine")).intValue() : 0);
        distribution.put("70-79", data.get("seventyToSeventyNine") != null ? ((BigDecimal) data.get("seventyToSeventyNine")).intValue() : 0);
        distribution.put("80-89", data.get("eightyToEightyNine") != null ? ((BigDecimal) data.get("eightyToEightyNine")).intValue() : 0);
        distribution.put("90-100", data.get("ninetyToHundred") != null ? ((BigDecimal) data.get("ninetyToHundred")).intValue() : 0);

        return distribution;
    }

    @Override
    public int insertGrades(List<Grade> gradeList) {
        return gradeMapper.insertGrades(gradeList);
    }



    /**
     * 方式二：获取所有成绩，在Java代码中进行统计（更灵活的方式）
     */
//    @Override
//    public List<Map<String, Object>> getGradeDistributionDetails() {
//        List<Grade> grades = gradeMapper.getAllGrades();
//        if (grades == null || grades.isEmpty()) {
//            return Collections.emptyList();
//        }
//
//        // 定义分数段
//        List<Map<String, Object>> ranges = Arrays.asList(
//                createRange("小于60", 0, 59),
//                createRange("60-69", 60, 69),
//                createRange("70-79", 70, 79),
//                createRange("80-89", 80, 89),
//                createRange("90-100", 90, 100)
//        );
//
//        // 统计各分数段人数
//        for (Grade grade : grades) {
//            BigDecimal finalGrade = grade.getFinalGrade();
//            if (finalGrade == null) continue;
//
//            double gradeValue = finalGrade.doubleValue();
//            for (Map<String, Object> range : ranges) {
//                int min = (int) range.get("min");
//                int max = (int) range.get("max");
//                if (gradeValue >= min && gradeValue <= max) {
//                    range.put("count", (int) range.get("count") + 1);
//                    break;
//                }
//            }
//        }
//
//        return ranges;
//    }
//
//    /**
//     * 创建分数段对象
//     */
//    private Map<String, Object> createRange(String name, int min, int max) {
//        Map<String, Object> range = new HashMap<>();
//        range.put("name", name);
//        range.put("min", min);
//        range.put("max", max);
//        range.put("count", 0);
//        return range;
//    }
}
